package com.refuel.controller;

import com.refuel.domain.Address;
import com.refuel.domain.Gift;
import com.refuel.domain.WinnerRecord;
import com.refuel.request.ExchangeRequest;
import com.refuel.response.PageResponse;
import com.refuel.response.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author zhanghao
 * @description
 * @date 2024/5/10 14:57
 **/
@RestController
@RequestMapping("/my")
@Tag(name = "我的控制器", description = "我的相关接口")
public class MyController {

    private static final Logger logger = LoggerFactory.getLogger(GoodsController.class);

    /**
     * 中奖核销接口
     * @return Response
     */
    @Operation(summary = "中奖核销接口")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "成功"),
            @ApiResponse(responseCode = "400", description = "xx参数错误")
    })
    @RequestMapping(method = RequestMethod.POST, path = "/gift/exchange")
    public Response exchange(@RequestBody ExchangeRequest request) {
        logger.info("exchange request: {}", request);
        Response response = new Response();

        return response;
    }

    /**
     * 我的礼券列表接口
     * @return PageResponse
     */
    @Operation(summary = "我的礼券列表接口")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "成功"),
            @ApiResponse(responseCode = "400", description = "xx参数错误")
    })
    @RequestMapping(method = RequestMethod.GET, path = "/gift/list")
    public PageResponse<Gift> giftList(@RequestParam(name = "uid") String uid) {
        logger.info("giftList uid: {}", uid);
        PageResponse<Gift> response = new PageResponse<>();

        response.addData(buildGift("100001"));
        response.addData(buildGift("100002"));
        response.addData(buildGift("100003"));
        response.addData(buildGift("100004"));
        response.addData(buildGift("100005"));
        return response;
    }

    /**
     * 我的中奖列表接口
     * @return PageResponse
     */
    @Operation(summary = "我的中奖列表接口")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "成功"),
            @ApiResponse(responseCode = "400", description = "xx参数错误")
    })
    @RequestMapping(method = RequestMethod.GET, path = "/win/list")
    public PageResponse<WinnerRecord> winnerList(@RequestParam(name = "uid") String uid) {
        logger.info("winnerList uid: {}", uid);
        PageResponse<WinnerRecord> response = new PageResponse<>();

        response.addData(buildWinnerRecord("一等奖"));
        response.addData(buildWinnerRecord("二等奖"));
        response.addData(buildWinnerRecord("三等奖"));
        response.addData(buildWinnerRecord("参与奖"));
        return response;
    }

    private WinnerRecord buildWinnerRecord(String winInfo) {
        WinnerRecord winnerRecord = new WinnerRecord();
        winnerRecord.setActivityId("123456");
        winnerRecord.setIntegral(10);
        winnerRecord.setUid("123456");
        winnerRecord.setTime(new Date());
        winnerRecord.setSeriesNo("20240501");
        winnerRecord.setWinInfo(winInfo);
        return winnerRecord;
    }

    private Gift buildGift(String giftId) {
        Gift gift = new Gift();
        gift.setId(giftId);
        gift.setName("加油卡");
        gift.setPrice(9.50);
        gift.setStartTime(new Date());
        gift.setEndTime(new Date());

        List<Address> addressList = new ArrayList<>();
        Address address = new Address();
        address.setId("123456");
        address.setName("北京王府井");
        address.setAddress("北京市东城区王府井大街256号");
        address.setPhone("010-12345678");
        address.setLongitude("39.90469");
        address.setLatitude("40.01234");
        addressList.add(address);
        address = new Address();
        address.setId("123456");
        address.setName("北京王府井");
        address.setAddress("北京市东城区王府井大街256号");
        address.setPhone("010-12345678");
        address.setLongitude("39.90469");
        address.setLatitude("40.01234");
        addressList.add(address);
        address = new Address();
        address.setId("123456");
        address.setName("北京王府井");
        address.setAddress("北京市东城区王府井大街256号");
        address.setPhone("010-12345678");
        address.setLongitude("39.90469");
        address.setLatitude("40.01234");
        addressList.add(address);
        gift.setAddressList(addressList);

        return gift;
    }
}
